package com.aaa.cs.controller;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import com.aaa.cs.entity.User;
import com.aaa.cs.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2020-12-01 14:40:22
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("通过主键查询单条数据")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }


    /**
     * 查询所有分页加条件查询
     *
     * @param pageNo   第几页
     * @param pageSize 每页显示数量
     * @param user     实体
     * @return Result
     */
    @PostMapping("queryAll")
    @ApiOperation("查询所有分页加条件查询")
    public Result queryAll(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody User user) {
        return this.userService.queryAll(pageNo, pageSize, user);
    }

    /**
     * 根据ID删除
     *
     * @param userId id
     * @return Result
     */
    @DeleteMapping("deleteById")
    @ApiOperation("根据ID删除")
    public Result deleteById(Integer userId) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userService.deleteById(userId));
    }

    /**
     * 更新
     *
     * @param @param user 实例对象
     * @return Result
     */
    @RequestMapping("update")
    @ApiOperation("更新")
    public Result update(@ApiParam("更新json数据") @RequestBody User user) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userService.update(user));
    }

    /**
     * 添加
     *
     * @param @param user 实例对象
     * @return Result
     */
    @PostMapping("insert")
    @ApiOperation("添加")
    public Result insert(@ApiParam("添加json数据") @RequestBody User user) {
        return this.userService.insert(user);
    }

    /**
     *查询用户id对应的角色id集合
     * @param userId
     * @return
     */
    @RequestMapping("listRoleIdsByUserId")
    public Result listRoleIdsByUserId(Integer userId){
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userService.listRoleIdsByUserId(userId));
    }

    /**
     * 添加用户角色关联
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("addUserAndRole")
    public Result addUserAndRole(Integer userId,String roleIds){
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.userService.addUserAndRole(userId,roleIds));
    }

    /**
     * 根据用户名查询对象
     * @param userName
     * @return
     */
    @GetMapping("queryByUserName")
    public User queryByUserName(@RequestParam("userName") String userName) {
        return userService.queryByUserName(userName);
    }



}